package com.cow.framework.core.message;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cow.framework.core.base.BaseComponent;


/**
 * 消息服务
 * 
 * @author yangsj
 * */
@Service
public class MessageService extends BaseComponent{
	@Autowired
	MessageDAO messageDAO;
	
	/**根据Id查询消息*/
	public Message getMessageById(Integer id){	
		return messageDAO.findOne(id);
	}
	
	/**检索消息*/
	public Page<Message> queryMessage(Map paramMap,Pageable pageable){	
		return messageDAO.findAll(pageable);
	}
	
	/**检索消息*/
	public Page<Message> getMyMessage(Pageable pageable){	
		return messageDAO.findByReceiver(super.getCurUserId(),pageable);
	}
	
	/**检索消息*/
	public List<Message> getNoPushMessage(Integer receiver){	
		return messageDAO.findByReceiverAndStatus(receiver,0);
	}
	
	/**检索消息*/
	public List<Message> getNoPushMessage(){	
		return messageDAO.findByStatus(0);
	}
	
	/**检索消息*/
	public Page<Message> getNoReadMessage(Pageable pageable){	
		return messageDAO.findNoRead(super.getCurUserId(),pageable);
	}
	
	/**新消息条数*/
	public Integer countNoRead(Integer receiver){	
		return messageDAO.countNoRead(receiver);
	}
	
	/**保存消息*/
	public Message saveMessage(Message message){
		return messageDAO.save(message);
	}
	
	/**保存消息*/
	public void saveMessage(List<Message> messageList){
		messageDAO.save(messageList);
	}
	
	/**删除过期消息*/
	@Transactional
	public void delExpiredMessage(){
		messageDAO.delExpiredMessage();
	}
	
}
